package com.extension.company.view.main;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseFragment;
import com.extension.company.common.utils.Utils;
import com.extension.company.contract.main.FragmentSameCityContract;
import com.extension.company.model.entity.CityNeedData;
import com.extension.company.presenter.main.FragmentSameCityPresenter;
import com.extension.company.view.adapter.CityNeedAdapter;
import com.extension.company.view.adapter.LocationAdapter;
import com.extension.company.view.localcity.LocalCitySecondActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作者:zwk
 * 联系方式：497981239@qq.com
 * 时间：on 2019/6/20 22:15
 * 功能：同城Fragment
 */
public class FragmentSameCity extends BaseFragment<FragmentSameCityContract.view, FragmentSameCityPresenter> implements FragmentSameCityContract.view {
	@BindView(R.id.same_city_top)
	LinearLayout sameCityTop;
	@BindView(R.id.mListView)
	ListView listView;
	@BindView(R.id.title_layout)
	View title_layout;


	private Context context;
	private View localHeader;
	private LocationAdapter locationAdapter;
	private RecyclerView recyclerView;
	private CityNeedAdapter cityNeedAdapter;
	private List<CityNeedData> list;

	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		this.context = context;
	}

	@Override
	public FragmentSameCityContract.view createView() {
		return this;
	}

	@Override
	public FragmentSameCityPresenter createPresenter() {
		return new FragmentSameCityPresenter();
	}

	@Override
	public void init() {
		initData();
		initView();


	}

	private void initData() {
		list = new ArrayList<>();
		CityNeedData cityNeedData1 = new CityNeedData();
		cityNeedData1.setImgPath(R.drawable.cityneed_house_service);
		cityNeedData1.setImgName("家政服务");

		CityNeedData cityNeedData2 = new CityNeedData();
		cityNeedData2.setImgPath(R.drawable.cityneed_decoration);
		cityNeedData2.setImgName("装修建材");

		CityNeedData cityNeedData3 = new CityNeedData();
		cityNeedData3.setImgPath(R.drawable.cityneed_edu);
		cityNeedData3.setImgName("教育培训");

		CityNeedData cityNeedData4 = new CityNeedData();
		cityNeedData4.setImgPath(R.drawable.cityneed_business);
		cityNeedData4.setImgName("商务服务");

		CityNeedData cityNeedData5 = new CityNeedData();
		cityNeedData5.setImgPath(R.drawable.cityneed_pets);
		cityNeedData5.setImgName("宠物服务");

		CityNeedData cityNeedData6 = new CityNeedData();
		cityNeedData6.setImgPath(R.drawable.cityneed_entertainment);
		cityNeedData6.setImgName("休闲娱乐");

		CityNeedData cityNeedData7 = new CityNeedData();
		cityNeedData7.setImgPath(R.drawable.cityneed_wedding_celebration);
		cityNeedData7.setImgName("婚庆服务");

		CityNeedData cityNeedData8 = new CityNeedData();
		cityNeedData8.setImgPath(R.drawable.cityneed_eat);
		cityNeedData8.setImgName("餐饮服务");

		CityNeedData cityNeedData9 = new CityNeedData();
		cityNeedData9.setImgPath(R.drawable.cityneed_car);
		cityNeedData9.setImgName("汽车服务");

		CityNeedData cityNeedData10 = new CityNeedData();
		cityNeedData10.setImgPath(R.drawable.cityneed_hotel);
		cityNeedData10.setImgName("旅游酒店");

		list.add(cityNeedData1);
		list.add(cityNeedData2);
		list.add(cityNeedData3);
		list.add(cityNeedData4);
		list.add(cityNeedData5);
		list.add(cityNeedData6);
		list.add(cityNeedData7);
		list.add(cityNeedData8);
		list.add(cityNeedData9);
		list.add(cityNeedData10);
	}

	@Override
	public int getMainLayout() {
		return R.layout.fragment_same_city;
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	/**
	 * 设置界面布局
	 */
	private void initView() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) sameCityTop.getLayoutParams();
			layoutParams.height = Utils.getStatusBarHeight(getActivity());  // 获取手机状态栏高度
			sameCityTop.setLayoutParams(layoutParams);
			sameCityTop.setBackgroundColor(getResources().getColor(R.color.topcolor));
		}
		sameCityTop.setAlpha(1.0f);
		title_layout.setAlpha(1.0f);

		localHeader = LayoutInflater.from(context).inflate(R.layout.fragment_location_city_headerview, null);
		listView.addHeaderView(localHeader);

		recyclerView = getViewById(localHeader, R.id.fragment_local_head_list);  // 同城服务
		cityNeedAdapter = new CityNeedAdapter(context, list);
		recyclerView.setAdapter(cityNeedAdapter);
		cityNeedAdapter.setContent(list);

		locationAdapter = new LocationAdapter(context);
		listView.setAdapter(locationAdapter);

		cityNeedAdapter.setRcyItemClick(new CityNeedAdapter.recycleViewOnclick() {
			@Override
			public void itemClick(int positon) {
				if (positon == 0) {
					LocalCitySecondActivity.startActivity((Activity) context);
				}
			}
		});

	}
}
